//
//  ScrollTableViewCell.m
//  Toyohu
//
//  Created by celia on 16/3/29.
//  Copyright (c) 2016年 celia. All rights reserved.
//

#import "ScrollTableViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIView+Fillet.h"
#import <AVFoundation/AVFoundation.h>


@interface ScrollTableViewCell ()
{
     AVAudioPlayer *player;
    UIImageView *voice;
    UIActivityIndicatorView *testActivityIndicator;
    NSCache *cache;
}


@end

@implementation ScrollTableViewCell

- (void)setImage:(UIImageView *)image
{
    [self.voiceButton addSubview:image];
}

- (void)awakeFromNib {
    // Initialization code
//    if (!testActivityIndicator) {
//        [self setAIV];
//    }
//    [self setCache];
    [self.headImage filletWithcornerRadius:self.headImage.frame.size.height / 2];
    
}

//- (void)setCache
//{
//    if (!cache) {
//        cache = [[NSCache alloc] init];
//    }
//}

//- (void)setAIV
//{
//    testActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
//    testActivityIndicator.center = self.center;
//    [self addSubview:testActivityIndicator];
//    testActivityIndicator.color = [UIColor redColor];
//    [testActivityIndicator startAnimating];
//    [testActivityIndicator setHidesWhenStopped:YES];
//}



- (void)setModelWithTitle:(NSString *)title nickName:(NSString *)nickName headUrl:(NSString *)url playerUrl:(NSString *)playerUrl
{
    self.titleLabel.text = title;
    self.nickName.text = nickName;
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"tuceng-21.png"]];
    
    if (!voice) {
        [self setImage];
    }
//    if (![cache objectForKey:@"playerData"]) {
//        [self performSelectorInBackground:@selector(getPlayerData:) withObject:playerUrl];
//    }else{
//        [self setPlayer:[cache objectForKey:@"playerData"]];
//    }
    
}


- (void)getPlayerData:(NSString *)playUrl
{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:playUrl]];
     if (data) {
        [cache setObject:data forKey:@"playerData"];
        [self setPlayer:data];
    }
   //  [cache setObject:data forKey:@"playerData"];
   // [self setPlayer:data];

 }



- (void)setImage
{
    UIImage *myImage = [UIImage imageNamed:@"icon_play02(1)"];
    voice = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 9, 13.5)];
    voice.center = CGPointMake(myImage.size.width, self.voiceButton.frame.size.height/2);
    NSMutableArray *imagesArray = [NSMutableArray array];
    for (int i = 0; i< 3; i++) {
        UIImage *Image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_play0%d(1)",i]];
        [imagesArray addObject:Image];
    }
    voice.image = [UIImage imageNamed:@"icon_play02(1)"];
    voice.animationImages = imagesArray;
    voice.animationDuration = 1;
    [self.voiceButton addSubview:voice];
}

-(void)playVoice{
    self.voiceButton.selected = !self.voiceButton.selected;
    if (!self.voiceButton.selected)
    {
        [voice startAnimating];
        [player play];
    }
    else
    {
        
        [voice  stopAnimating];
        [player pause];
    }
}

- (void)setPlayer:(NSData *)data
{
    [testActivityIndicator stopAnimating];
    self.voiceButton.enabled = YES;

    player = [[AVAudioPlayer alloc] initWithData:data error:nil];
    player.volume = 1.0f;
    player.numberOfLoops = 0;
    [player prepareToPlay];
    CGFloat fen = player.duration/60;
    NSInteger time = player.duration;
    CGFloat miao = time % 60;
    self.timeLabel.text = [NSString stringWithFormat:@"%.f'%.f''",fen,miao];

}

//- (void)getPlayerData:(NSString *)playUrl
//{
//    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:playUrl]];
//     if (data) {
//        [cache setObject:data forKey:@"playerData"];
//        [self setPlayer:data];
//    }
//   //  [cache setObject:data forKey:@"playerData"];
//   // [self setPlayer:data];
//
//
//}

//- (void)setImage
//{
//    UIImage *myImage = [UIImage imageNamed:@"icon_play02(1)"];
//    voice = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 9, 13.5)];
//    voice.center = CGPointMake(myImage.size.width, self.voiceButton.frame.size.height/2);
//    NSMutableArray *imagesArray = [NSMutableArray array];
//    for (int i = 0; i< 3; i++) {
//        UIImage *Image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_play0%d(1)",i]];
//        [imagesArray addObject:Image];
//    }
//    voice.image = [UIImage imageNamed:@"icon_play02(1)"];
//    voice.animationImages = imagesArray;
//    voice.animationDuration = 1;
//    [self.voiceButton addSubview:voice];
//}

//-(void)playVoice{
//    self.voiceButton.selected = !self.voiceButton.selected;
//    if (!self.voiceButton.selected)
//    {
//        [voice startAnimating];
//        [player play];
//    }
//    else
//    {
//        
//        [voice  stopAnimating];
//        [player pause];
//    }
//}

//- (void)setPlayer:(NSData *)data
//{
//    [testActivityIndicator stopAnimating];
//    self.voiceButton.enabled = YES;
//    player = [[AVAudioPlayer alloc] initWithData:data error:nil];
//    player.volume = 1.0f;
//    player.numberOfLoops = 0;
//    [player prepareToPlay];
//    CGFloat fen = player.duration/60;
//    NSInteger time = player.duration;
//    CGFloat miao = time % 60;
//    self.timeLabel.text = [NSString stringWithFormat:@"%.f'%.f''",fen,miao];
//
//}


@end
